package com.lbt.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lbt.commonuntils.R;
import com.lbt.eduservice.entity.EduTeacher;
import com.lbt.eduservice.entity.vo.TeacherQuery;
import com.lbt.eduservice.service.EduTeacherService;
import com.lbt.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-05-26
 */
@Api(description = "讲师管理")
@RestController
//@CrossOrigin
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
    @Autowired
    EduTeacherService teacherService;

    //1 查询讲师表所有数据
    @ApiOperation(value ="查询所有讲师" )
    //1、查询数据操作 Restful  风格
    @GetMapping("/findAll")
    public R getTeachers(){
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items",list);
    }

    //2、删除讲师
    @ApiOperation(value = "逻辑删除讲师" )
    @DeleteMapping("/{id}")
    public R removeTeacher(@ApiParam(name = "id",value = "讲师ID",required = true) @PathVariable(value = "id")String id){
        boolean b = teacherService.removeById(id);
        if(b){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("删除失败");
        }
    }

    //3、分页查询
    /*分页查询，条件查询
    * */
    @ApiOperation(value = "分页查询讲师" )
    @GetMapping("/pageTeacher/{current}/{limit}")
    public R selectByTeacher(@PathVariable("current")Integer cruent,
                             @PathVariable("limit")Integer limit){


        Page<EduTeacher> pageTeacher=new Page<>(cruent,limit);
        teacherService.page(pageTeacher, null);
        //注意他是将结果直接存进 pageTeacher
        List<EduTeacher> records = pageTeacher.getRecords();
        return R.ok().data("total",pageTeacher.getTotal()).data("data",pageTeacher.getRecords());
    }

//    //添加讲师接口的方法
//    @PostMapping("addTeacher")
//    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
//        boolean save = teacherService.save(eduTeacher);
//        if(save) {
//            return R.ok();
//        } else {
//            return R.error();
//        }
//    }

    /*
    * 添加讲师  4、添加讲师
    * */
    @ApiOperation(value = "添加讲师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean result = teacherService.save(eduTeacher);
        if(result){
            return R.ok();
        }else {
            return R.error();
        }

    }

    //5、根据ID查询讲师
    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("/getTeacher/{id}")
    public R getById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){

        EduTeacher teacher = teacherService.getById(id);
        return R.ok().data("teacher", teacher);
    }

//    注意如果使用PUT 提交数据ID  就无法封装进 teacher对象 需要手动set进去
    @ApiOperation(value = "修改讲师信息")
    //讲师修改功能
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean flag = teacherService.updateById(eduTeacher);
        if(flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

//    //讲师修改功能
//    @PostMapping("updateTeacher")
//    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
//        boolean flag = teacherService.updateById(eduTeacher);
//        if(flag) {
//            return R.ok();
//        } else {
//            return R.error();
//        }
//    }


    @ApiOperation(value = "根据条件实现多条件，分页查询讲师" )
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondation( @ApiParam(name = "current", value = "开始页", required = true) @PathVariable Long current,
                                   @ApiParam(name = "limit", value = "每页显示数据量", required = true) @PathVariable Long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        //创建page对象
        Page<EduTeacher> pageTeacher=new Page<>(current,limit);
        //  构建条件，多条件查询
        QueryWrapper<EduTeacher> wrapper=new QueryWrapper<>();
        //和动态 sql 差不多

        //判断条件值是否为空，如果不为空就加入条件
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //模糊查询
        if(!StringUtils.isEmpty(name)){
            //构建他的条件
            wrapper.like("name",name);
        }
        //匹配查询
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        //大于开始时间 ge是大于等于 gt是大于
        if(!StringUtils.isEmpty(begin)){
            wrapper.gt("gmt_create",begin);
        }
        //小于结束时间 le 小于等于
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_modified",end);
        }
        //调用方法实现分页查询
        teacherService.page(pageTeacher,wrapper);

        Long total=pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        return R.ok().data("total",total).data("rows",records);

    }

}

